home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / TRISPD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-26  |  6.7 KB  |  265 lines

  1. /* $Id: trispd.c,v 3.2 1998/07/08 03:02:00 brianp Exp $ */
  2.  
  3. /*
  4.  * Simple GLUT program to measure triangle strip rendering speed.
  5.  * Brian Paul  February 15, 1997  This file is in the public domain.
  6.  */
  7.  
  8. /*
  9.  * $Log: trispd.c,v $
  10.  * Revision 3.2  1998/07/08 03:02:00  brianp
  11.  * added Marten Stromberg's texture options
  12.  *
  13.  * Revision 3.1  1998/06/29 02:36:58  brianp
  14.  * removed unneeded includes
  15.  *
  16.  * Revision 3.0  1998/02/14 18:42:29  brianp
  17.  * initial rev
  18.  *
  19.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <GL/glut.h>
  26.  
  27.  
  28. static float MinPeriod = 2.0;   /* 2 seconds */
  29. static float Width = 400.0;
  30. static float Height = 400.0;
  31. static int Loops = 1;
  32. static int Size = 50;
  33. static int Texture = 0;
  34.  
  35.  
  36.  
  37. static void Idle( void )
  38. {
  39.    glutPostRedisplay();
  40. }
  41.  
  42.  
  43. static void Display( void )
  44. {
  45.    float x, y;
  46.    float xStep;
  47.    float yStep;
  48.    double t0, t1;
  49.    double triRate;
  50.    double pixelRate;
  51.    int triCount;
  52.    int i;
  53.    float red[3] = { 1.0, 0.0, 0.0 };
  54.    float blue[3] = { 0.0, 0.0, 1.0 };
  55.  
  56.    xStep = yStep = sqrt( 2.0 * Size );
  57.  
  58.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  59.  
  60.    triCount = 0;
  61.    t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  62.    if (Texture) {
  63.       float uStep = xStep / Width;
  64.       float vStep = yStep / Height;
  65.       float u, v;
  66.       for (i=0; i<Loops; i++) {
  67.      for (y=1.0, v=0.0f; y<Height-yStep; y+=yStep, v+=vStep) {
  68.         glBegin(GL_TRIANGLE_STRIP);
  69.         for (x=1.0, u=0.0f; x<Width; x+=xStep, u+=uStep) {
  70.            glColor3fv(red);
  71.            glTexCoord2f(u, v);        
  72.            glVertex2f(x, y);
  73.            glColor3fv(blue);
  74.            glTexCoord2f(u, v+vStep);
  75.            glVertex2f(x, y+yStep);
  76.            triCount += 2;
  77.         }
  78.         glEnd();
  79.         triCount -= 2;
  80.      }
  81.       }
  82.    }
  83.    else {
  84.       for (i=0; i<Loops; i++) {
  85.      for (y=1.0; y<Height-yStep; y+=yStep) {
  86.         glBegin(GL_TRIANGLE_STRIP);
  87.         for (x=1.0; x<Width; x+=xStep) {
  88.            glColor3fv(red);
  89.            glVertex2f(x, y);
  90.            glColor3fv(blue);
  91.            glVertex2f(x, y+yStep);
  92.            triCount += 2;
  93.         }
  94.         glEnd();
  95.         triCount -= 2;
  96.      }
  97.       }
  98.    }
  99.    t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  100.    
  101.    if (t1-t0 < MinPeriod) {
  102.       /* Next time draw more triangles to get longer elapsed time */
  103.       Loops *= 2;
  104.       return;
  105.    }
  106.  
  107.    triRate = triCount / (t1-t0);
  108.    pixelRate = triRate * Size;
  109.    printf("Rate: %d tri in %gs = %g tri/s  %d pixels/s\n",
  110.           triCount, t1-t0, triRate, (int)pixelRate);
  111.  
  112.    glutSwapBuffers();
  113. }
  114.  
  115.  
  116. static void Reshape( int width, int height )
  117. {
  118.    Width = width;
  119.    Height = height;
  120.    glViewport( 0, 0, width, height );
  121.    glMatrixMode( GL_PROJECTION );
  122.    glLoadIdentity();
  123.    glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
  124.    glMatrixMode( GL_MODELVIEW );
  125.    glLoadIdentity();
  126. }
  127.  
  128.  
  129. static void Key( unsigned char key, int x, int y )
  130. {
  131.    switch (key) {
  132.       case 27:
  133.          exit(0);
  134.          break;
  135.    }
  136.    glutPostRedisplay();
  137. }
  138.  
  139.  
  140. static void LoadTex(int comp, int filter)
  141. {
  142.    GLubyte *pixels;
  143.    int x, y;
  144.    pixels = malloc(4*256*256);
  145.    for (y = 0; y < 256; ++y)
  146.       for (x = 0; x < 256; ++x) {
  147.      pixels[(y*256+x)*4+0] = (int)(128.5 + 127.0 * cos(0.024544 * x));
  148.      pixels[(y*256+x)*4+1] = 255;
  149.      pixels[(y*256+x)*4+2] = (int)(128.5 + 127.0 * cos(0.024544 * y));
  150.      pixels[(y*256+x)*4+3] = 255;
  151.       }
  152.    glEnable(GL_TEXTURE_2D);
  153.    glTexImage2D(GL_TEXTURE_2D, 0, comp, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  154.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
  155.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
  156.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  157.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  158.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  159.    printf("Texture: GL_MODULATE, %d comps, %s\n", comp, filter == GL_NEAREST ? "GL_NEAREST" : "GL_LINEAR");
  160. }
  161.  
  162.  
  163. static void Init( int argc, char *argv[] )
  164. {
  165.    GLint shade;
  166.    GLint rBits, gBits, bBits;
  167.    int filter = GL_NEAREST, comp = 3;
  168.  
  169.    int i;
  170.    for (i=1; i<argc; i++) {
  171.       if (strcmp(argv[i],"-dither")==0)
  172.          glDisable(GL_DITHER);
  173.       else if (strcmp(argv[i],"+dither")==0)
  174.          glEnable(GL_DITHER);
  175.       else if (strcmp(argv[i],"+smooth")==0)
  176.          glShadeModel(GL_SMOOTH);
  177.       else if (strcmp(argv[i],"+flat")==0)
  178.          glShadeModel(GL_FLAT);
  179.       else if (strcmp(argv[i],"+depth")==0)
  180.          glEnable(GL_DEPTH_TEST);
  181.       else if (strcmp(argv[i],"-depth")==0)
  182.          glDisable(GL_DEPTH_TEST);
  183.       else if (strcmp(argv[i],"-size")==0) {
  184.          Size = atoi(argv[i+1]);
  185.          i++;
  186.       }
  187.       else if (strcmp(argv[i],"-texture")==0)
  188.      Texture = 0;
  189.       else if (strcmp(argv[i],"+texture")==0)
  190.      Texture = 1;
  191.       else if (strcmp(argv[i],"-linear")==0)
  192.      filter = GL_NEAREST;
  193.       else if (strcmp(argv[i],"+linear")==0)
  194.      filter = GL_LINEAR;
  195.       else if (strcmp(argv[i],"-persp")==0)
  196.      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  197.       else if (strcmp(argv[i],"+persp")==0)
  198.      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  199.       else if (strcmp(argv[i],"-comp")==0) {
  200.      comp = atoi(argv[i+1]);
  201.      i++;
  202.       }
  203.       else
  204.          printf("Unknown option: %s\n", argv[i]);
  205.    }
  206.  
  207.    glGetIntegerv(GL_SHADE_MODEL, &shade);
  208.  
  209.    printf("Dither: %s\n", glIsEnabled(GL_DITHER) ? "on" : "off");
  210.    printf("ShadeModel: %s\n", (shade==GL_FLAT) ? "flat" : "smooth");
  211.    printf("DepthTest: %s\n", glIsEnabled(GL_DEPTH_TEST) ? "on" : "off");
  212.    printf("Size: %d pixels\n", Size);
  213.  
  214.    if (Texture)
  215.       LoadTex(comp, filter);
  216.  
  217.    glGetIntegerv(GL_RED_BITS, &rBits);
  218.    glGetIntegerv(GL_GREEN_BITS, &gBits);
  219.    glGetIntegerv(GL_BLUE_BITS, &bBits);
  220.    printf("RedBits: %d  GreenBits: %d  BlueBits: %d\n", rBits, gBits, bBits);
  221. }
  222.  
  223.  
  224. static void Help( const char *program )
  225. {
  226.    printf("%s options:\n", program);
  227.    printf("  +/-dither      enable/disable dithering\n");
  228.    printf("  +/-depth       enable/disable depth test\n");
  229.    printf("  +flat          flat shading\n");
  230.    printf("  +smooth        smooth shading\n");
  231.    printf("  -size pixels   specify pixels/triangle\n");
  232.    printf("  +/-texture     enable/disable texture\n");
  233.    printf("  -comp n        texture format\n");
  234.    printf("  +/-linear      bilinear texture filter\n");
  235.    printf("  +/-persp       perspective correction hint\n");
  236. }
  237.  
  238.  
  239. int main( int argc, char *argv[] )
  240. {
  241.    printf("For options:  %s -help\n", argv[0]);
  242.    glutInit( &argc, argv );
  243.    glutInitWindowSize( (int) Width, (int) Height );
  244.    glutInitWindowPosition( 0, 0 );
  245.  
  246.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  247.  
  248.    glutCreateWindow( argv[0] );
  249.  
  250.    if (argc==2 && strcmp(argv[1],"-help")==0) {
  251.       Help(argv[0]);
  252.       return 0;
  253.    }
  254.  
  255.    Init( argc, argv );
  256.  
  257.    glutReshapeFunc( Reshape );
  258.    glutKeyboardFunc( Key );
  259.    glutDisplayFunc( Display );
  260.    glutIdleFunc( Idle );
  261.  
  262.    glutMainLoop();
  263.    return 0;
  264. }
  265.